package com.lianniu.web.favorite.service.impl;

import com.lianniu.web.base.BaseResult;
import com.lianniu.web.buyerProduct.service.IProductService;
import com.lianniu.web.constants.ErrorCode;
import com.lianniu.web.favorite.dao.FavoriteDao;
import com.lianniu.web.favorite.model.FavoriteVo;
import com.lianniu.web.favorite.service.FavoriteService;
import com.lianniu.web.product.model.Page;
import com.lianniu.web.product.model.SkuProductVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017-05-07.
 */
@Service
@Transactional
public class FavoriteServiceImpl implements FavoriteService{
    Logger logger = LoggerFactory.getLogger(FavoriteServiceImpl.class);
    @Autowired
    FavoriteDao favoriteDao;

    @Override
    public BaseResult isFavorite(FavoriteVo vo) {
        vo.setDelFlag("1");
        int c = favoriteDao.isFavorite(vo);
        return BaseResult.success(c > 0?"yes":"no");
    }

    @Override
    public BaseResult addToFavorite(FavoriteVo vo) {
        FavoriteVo favoriteVo = this.favoriteDao.getFavorite(vo.getcusUserId(),vo.getSupplyProductId());
        if (favoriteVo != null){
            if (favoriteVo.getDelFlag().equals("1")){
                favoriteVo.setDelFlag("0");
                this.updateFavoriteDelFlag(favoriteVo);
            }else{
                favoriteVo.setDelFlag("1");
                this.updateFavoriteDelFlag(favoriteVo);
            }
            return BaseResult.success();
        }
        vo.setDelFlag("1");
        boolean re = favoriteDao.addToFavorite(vo);
        return BaseResult.success(re);
    }

    @Override
    public BaseResult updateFavoriteDelFlag(FavoriteVo vo) {
        boolean update = this.favoriteDao.updateFavoriteDelFlag(vo);
        return BaseResult.success();
    }

    @Override
    public BaseResult getFavoritesProduct(String userId,int pageNum) {
        Map<String, Object> param = new HashMap<>();
        param.put("userId", userId);

        int count = 0;
        Page page =null;

        try {
            count = favoriteDao.getFavoriteProductCount(param);
        } catch (Exception e) {
            e.printStackTrace();
            logger.info(ErrorCode.SQLExceptionRetInfo);
            return BaseResult.exception(ErrorCode.SQLExceptionRetInfo);
        }

        page = new Page(count, pageNum);
        param.put("page", page);

        List<SkuProductVo> list = null;

        try {
            if (pageNum <= page.getPageSum()) {
                list = favoriteDao.getFavoritesProduct(param);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.info(ErrorCode.SQLExceptionRetInfo);
            return BaseResult.exception(ErrorCode.SQLExceptionRetInfo);
        }

        logger.info("ProductServiceImpl getList end");
        return BaseResult.success(list);
    }
}
